package com.party.admin.biz.enterpriseservice;

import com.party.admin.biz.file.FileBizService;
import com.party.core.utils.MyBeanUtils;
import com.party.common.utils.LangUtils;
import com.party.common.utils.StringUtils;
import com.party.core.model.enterpriseservice.EpService;
import com.party.core.model.enterpriseservice.EpServiceCategory;
import com.party.core.service.enterpriseservice.IEpServiceCategoryService;
import com.party.core.service.enterpriseservice.IEpServiceService;
import org.apache.commons.lang3.StringEscapeUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Arrays;
import java.util.List;

/**
 * @program: party
 * @description: 企业服务业务
 * @author: Hua Qunhao
 * @create: 2019-01-09 18:19
 */
@Service("EpServiceBizService")
public class EpServiceBizService {

    @Autowired
    private IEpServiceService epServiceService;

    @Autowired
    private IEpServiceCategoryService epServiceCategoryService;

    @Autowired
    private FileBizService fileBizService;


    Logger logger = LoggerFactory.getLogger(EpServiceBizService.class);

    public void saveOrupdateEpservice(EpService epService) throws Exception {

        if (StringUtils.isNotEmpty(epService.getContent())) {
            String content = StringUtils.escapeHtml4Iframe2Video(epService.getContent().trim());
            epService.setContent(content);
        }

        if (StringUtils.isNotEmpty(epService.getId())) {
            EpService t = epServiceService.get(epService.getId());
            MyBeanUtils.copyBeanNotNull2Bean(epService, t);// 将编辑表单中的非NULL值覆盖数据库记录中的值
            epServiceService.update(t);

            if (StringUtils.isNotEmpty(epService.getCategoryList()) && epService.getCategoryList().length() > 0) {

                String categoryids = epService.getCategoryList();
                //添加没有的类型
                for (String categoryId : categoryids.split(",")) {
                    EpServiceCategory category = epServiceCategoryService.findByCategorybycIdandsId(new EpServiceCategory(categoryId, epService.getId()));
                    if (category == null) {
                        category = new EpServiceCategory(categoryId, epService.getId());
                        epServiceCategoryService.insert(category);
                    }
                }
                //删除没有的类型
                List<EpServiceCategory> categorylist = epServiceCategoryService.list(new EpServiceCategory(null, epService.getId()));
                List<String> oldCategories = LangUtils.transform(categorylist, input -> input.getCategoryId());

                List<String> newCategories = Arrays.asList(categoryids.split(","));
                oldCategories.removeAll(newCategories);


                for (String oldPic : oldCategories) {
                    epServiceCategoryService.deleteEpCategoryBycIdandsid(new EpServiceCategory(oldPic, epService.getId()));
                }
            }

        } else {

            epServiceService.insert(epService);
            if (StringUtils.isNotEmpty(epService.getCategoryList()) && epService.getCategoryList().length() > 0) {
                for (String categoryId : epService.getCategoryList().split(",")) {

                    epServiceCategoryService.insert(new EpServiceCategory(categoryId, epService.getId()));
                }
            }
        }

        try {
            String filePath = fileBizService.getUploadCIPath(epService.getCreateBy(), "epservice");

            String fileName = "epservice_info_" + epService.getId();
            String content = "epservice/info.html?id=" + epService.getId();
            fileBizService.uploadCloudOut(fileName, filePath, content, epService.getCreateBy());
        } catch (Exception e) {
            logger.error("上传服务二维码异常", e);
        }
    }
}
